#include "Socket.hpp"
#include <functional>
using func_t = std::function<std::string(std::string &package)>;
class TcpServer
{
public:
    void InIt(const uint16_t &port = 0, const std::string &ip = "")
    {
        socket_.CreatSocket();
        if (!ip.empty())
        {
            socket_.Bind(port, ip);
        }
        else
        {
            socket_.Bind();
        }
        socket_.Listen();
    }

    void Run(func_t func)
    {
        SocketInfo client;
        std::string recive_str;
        std::string package_str;
        socket_.Accept(client);
        while (true)
        {

            socket_.Recive(client.socketfd_, recive_str);
            package_str += recive_str;
            recive_str = func(package_str);
            lg.logmessage(Debug,"ret:%s",recive_str.c_str());
            if (recive_str.empty())
                continue;
            socket_.Send(client.socketfd_, recive_str);
            continue;
        }
    }

private:
    Socket socket_;
};